const { cloud } = getApp();
Page({
  data: {
    level: '',
    goodlist: [],
    couponList: []
  },
  onLoad(e) {
    this.setData({
      level: e.level
    })
    this.loadGood();
    this.loadCoupon()
  },
  async chooseGood(e) {
    let index = e.currentTarget.dataset.index;
    let goodInfo = this.data.goodList[index];
    if (this.data.level == '1') {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/pool',
        method: 'POST',
        params: {
          shop_id: getApp().globalData.shop_id,
          name1: goodInfo.title,
          image1: goodInfo.pic_url,
          activity_url3: ''
        }
      })
      my.alert({
        content: '一等奖设置成功，返回之后再次刷新页面可以看到设置效果',
        success: (res) => {
          my.navigateTo({
            url: '/pages/index/index'
          });
        }
      })
    }
    if (this.data.level == '2') {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/pool',
        method: 'POST',
        params: {
          shop_id: getApp().globalData.shop_id,
          name2: goodInfo.title,
          image2: goodInfo.pic_url,
          activity_url3: ''
        }
      })
      my.alert({
        content: '二等奖设置成功，返回之后再次刷新页面可以看到设置效果',
        success: (res) => {
          my.navigateTo({ url: '/pages/index/index' });
        }
      })
    }
    if (this.data.level == '3') {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/pool',
        method: 'POST',
        params: {
          shop_id: getApp().globalData.shop_id,
          name3: goodInfo.title,
          image3: goodInfo.pic_url,
          activity_url3: ''
        }
      })
      my.alert({
        content: '三等奖设置成功，返回之后再次刷新页面可以看到设置效果',
        success: (res) => {
          my.navigateTo({ url: '/pages/index/index' });
        }
      })
    }
    // my.navigateTo({
    //   url: `/pages/index/index?goodInfo=${JSON.stringify(goodInfo)}`
    // });
  },
  async chooseCoupon(e) {
    let index = e.currentTarget.dataset.index;
    let couponInfo = this.data.couponList[index];
    if (this.data.level == '1') {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/pool',
        method: 'POST',
        params: {
          shop_id: getApp().globalData.shop_id,
          name1: `${couponInfo.denominations / 100}元优惠券`,
          image1: 'http://cdn.mzyun.tech/coupon.jpeg',
          activity_url1: couponInfo.activity_url
        }
      })
      my.alert({
        content: '一等奖设置成功，返回之后再次刷新页面可以看到设置效果',
        success: (res) => {
          my.navigateTo({ url: '/pages/index/index' });
        }
      })
    }
    if (this.data.level == '2') {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/pool',
        method: 'POST',
        params: {
          shop_id: getApp().globalData.shop_id,
          name2: `${couponInfo.denominations / 100}元优惠券`,
          image2: 'http://cdn.mzyun.tech/coupon.jpeg',
          activity_url2: couponInfo.activity_url
        }
      })
      my.alert({
        content: '二等奖设置成功，返回之后再次刷新页面可以看到设置效果',
        success: (res) => {
          my.navigateTo({ url: '/pages/index/index' });
        }
      })
    }
    if (this.data.level == '3') {
      const res = await cloud.application.httpRequest({
        path: '/index.php/index/qn/pool',
        method: 'POST',
        params: {
          shop_id: getApp().globalData.shop_id,
          name3: `${couponInfo.denominations / 100}元优惠券`,
          image3: 'http://cdn.mzyun.tech/coupon.jpeg',
          activity_url3: couponInfo.activity_url
        }
      })
      my.alert({
        content: '三等奖设置成功，返回之后再次刷新页面可以看到设置效果',
        success: (res) => {
          my.navigateTo({ url: '/pages/index/index' });
        }
      })
    }
    // my.navigateTo({
    //   url: `/pages/index/index?couponInfo=${JSON.stringify(couponInfo)}`
    // });
  },
  async loadGood() {
    try {
      const res = await cloud.topApi.invoke({
        api: 'taobao.items.onsale.get',
        authScope: '*',
        data: { fields: 'title,pic_url,price' }
      });
      this.setData({
        goodList: res.items
      })
    } catch (e) {
      my.alert({ content: 'error ' + e.message })
    }
  },
  async loadCoupon() {
    try {
      const res = await cloud.topApi.invoke({
        api: 'taobao.promotion.activity.get',
        authScope: '*'
      });
      let couponList = res.activitys;
      // couponList.forEach(
      //   async item => {
      //   const res2 = await cloud.topApi.invoke({
      //     api: 'taobao.promotion.coupons.get',
      //     authScope: '*',
      //     data: {
      //       coupon_id: item.coupon_id
      //     }
      //   });
      //   item.denominations=res2.coupons[0].denominations;
      // });
      for (let i = 0; i < couponList.length; i++) {
        const res2 = await cloud.topApi.invoke({
          api: 'taobao.promotion.coupons.get',
          authScope: '*',
          data: {
            coupon_id: couponList[i].coupon_id
          }
        });
        couponList[i].denominations=res2.coupons[0].denominations
      }
      this.setData({
        couponList: couponList
      })
    } catch (e) {
      my.alert({ content: 'error ' + e.message })
    }
  },
  createCoupon() {
    my.qn.navigateToWebPage({
      url: 'https://aliyx.taobao.com/coupon',
      success: (res) => { },
      fail: (res) => { },
    });
  }

});
